package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.provider.api.model.vo.ProductStockDetailsVo;
import com.weixing.mall.provider.mapper.ProductStockDetailsMapper;
import com.weixing.mall.provider.model.domain.Depot;
import com.weixing.mall.provider.model.domain.ProductStockDetails;
import com.weixing.mall.provider.model.query.ProductStockQuery;
import com.weixing.mall.provider.model.vo.ProductStockListVo;
import com.weixing.mall.provider.service.IDepotService;
import com.weixing.mall.provider.service.IProductStockDetailsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Small
 * @since 2021-07-30
 */
@Service
public class ProductStockDetailsServiceImpl extends ServiceImpl<ProductStockDetailsMapper, ProductStockDetails> implements IProductStockDetailsService {
  @Autowired
  private IDepotService depotService;
  @Autowired
  private  ProductStockDetailsMapper productStockDetailsMapper;
    @Override
    public Map<String, Object> totalQuantityAndAmount(ProductStockQuery productStockQuery) {
        return baseMapper.totalQuantityAndAmount(productStockQuery);
    }

    @Override
    public IPage<ProductStockListVo> selectOfPage(Page initMpPage, ProductStockQuery productStockQuery) {
        return baseMapper.selectOfPage(initMpPage,productStockQuery);
    }

    /**
     * 返回仓库的库存信息
     * @param depotId
     * @return
     */
    @Override
    public List<ProductStockDetailsVo> getAllAvaByDepotId(Long depotId) {
         List<ProductStockDetails> tmpList=baseMapper.selectList(new QueryWrapper<ProductStockDetails>().eq("depot_id",depotId).eq("is_lock", WhetherEnum.NO.getValue()));
         List<ProductStockDetailsVo>  result= tmpList.stream()
                 .map(e -> {
                     ProductStockDetailsVo d = new ProductStockDetailsVo();
                     BeanUtils.copyProperties(e, d);
                     d.setStock(1);
                     return d;
                 })
                 .collect(Collectors.toList());
        return result;
    }

    /**
     * 根据颜色、尺码和商品goodsId 匹配仓库信息
     * @param goodsId  商品ID
     * @param color  颜色
     * @param size 尺码
     * @return
     */
    @Override
    public ProductStockDetailsVo matching(Long goodsId, String color, String size) {
        List<ProductStockDetails> tmpList=baseMapper.selectList(new QueryWrapper<ProductStockDetails>().eq("goods_id",goodsId).like("product_sku_desp", color).like("product_sku_desp", size).orderByAsc("depot_id"));
        ProductStockDetailsVo productStockDetailsVo=new ProductStockDetailsVo();

        if(tmpList.size()!=0){
            BeanUtils.copyProperties(tmpList.get(0),productStockDetailsVo);
            Depot depot=depotService.getById(tmpList.get(0).getDepotId());
            productStockDetailsVo.setDepotName(null==depot?"":depot.getDepotName());
            return productStockDetailsVo;
        }
        return null;
    }

    @Override
    public List<ProductStockListVo> selectList(ProductStockQuery productStockQuery) {
        return productStockDetailsMapper.selectOfPage(productStockQuery);
    }

    /**
     * 按照库存编码批量删除库存信息
     * @param depotSn
     */
    @Override
    public void delByDepotSn(String depotSn) {
         productStockDetailsMapper.delete(new QueryWrapper<ProductStockDetails>().eq("stock_sn", depotSn));
    }

    @Override
   public Map<String, Object> test(String color, String size, String styleNo) {
               return productStockDetailsMapper.testinit(color,size,styleNo);
    }
}
